package com.itsci.assignment2;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class GuideManager extends SQLiteOpenHelper{

	public GuideManager(Context context, String name, CursorFactory factory,
			int version) {
		super(context, "Guide", null, version);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE Guide(guideId INTEGER  PRIMARY KEY AUTOINCREMENT ,NAME TEXT,tel text,email text);");
		
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE Guide");
		onCreate(db);
		
	}
	
	public void insertGuide(GuideBean guide) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues content = new ContentValues();
		content.put("name", guide.getGuidName());
		content.put("tel", guide.getTel());
		content.put("email",guide.getEmail());
		db.insert("Guide", null, content);
		db.close();
	}
	
	public void updateGuide(GuideBean guide) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues content = new ContentValues();
		content.put("name", guide.getGuidName());
		content.put("tel", guide.getTel());
		content.put("email",guide.getEmail());
		db.update("Guide", content, "guideId = ?", new String[] { guide.getId()+"" });
		db.close();
	}
	
	public void deleteGuide(int id) {
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete("Guide", "guideId = ?", new String[] { id+"" });
		db.close();
	}
	public int getId(String name){
		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.query("Guide", new String[] { "guideId","name","tel","email"},
				"name = ?", new String[] { name }, null, null, null);
		int id = 0;
		if (cursor.moveToFirst()) {
		 id = Integer.parseInt(cursor.getString(0));
		}
		
		return id;
	}
	
	public GuideBean getByGuideId(String name) {
		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.query("Guide", new String[] { "guideId","name","tel","email"},
				"name = ?", new String[] { name }, null, null, null);
		GuideBean guide = new GuideBean();
		if (cursor.moveToFirst()) {
			guide.setGuidName(cursor.getString(1));
			guide.setTel(cursor.getString(2));
			guide.setEmail(cursor.getString(3));
		} else {
			guide.setGuidName("");
			guide.setTel("");
			guide.setEmail("");
	
		}
		db.close();
		return guide;
	}
	
	public ArrayList<GuideBean> getAllRegisterBean() {
		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.query("Guide", new String[] {"guideId","name","tel","email"}, null, null, null, null, null);
		ArrayList<GuideBean> arrayGuide = new ArrayList<GuideBean>();
		if (cursor.moveToFirst()) {
			do {
				GuideBean guide = new GuideBean();				
				guide.setGuidName(cursor.getString(1));
				guide.setTel(cursor.getString(2));
				guide.setEmail(cursor.getString(3));
				arrayGuide.add(guide);
			}while(cursor.moveToNext());
		}
		db.close();
		return arrayGuide;
	}

}
